package com.ohos.carbon.slice.dialog;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DemoDataBean;
import com.ohos.carbon.provider.DemosAdapter;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.window.dialog.CommonDialog;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;

public class ListDialogActivity extends AbilitySlice {

    public Image back_button, image1, image2;
    public Text text_title;
    private ListContainer listContainer;
    private ArrayList<DemoDataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_listdialog);
        final TextField titleText = (TextField) findComponentById(ResourceTable.Id_titleText3);
        initView();

        Button button = (Button) findComponentById(ResourceTable.Id_button1);
        CommonDialog commonDialog = new CommonDialog(getContext());
        commonDialog.setSize(1000, MATCH_CONTENT);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                component = LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_listdialog, null, true);
                Text text = (Text) component.findComponentById(ResourceTable.Id_listText);
                text.setText(titleText.getText());
//                CommonDialog commonDialog = new CommonDialog(getContext());
                commonDialog.setContentCustomComponent(component);
                mList = new ArrayList<>();
                for (int i = 0; i < 4; i++) {
                    mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background1, null, "Henry Russell", "Lorem ipsum dolor sit amet", "21:42,04 3月"));
                    mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background2, null, "Heather Doris Russell", "Proin nibh augue , suscipit a", "21:54,04 3月"));
                    mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background3, null, "Diane Beverly Martinez", "Cras vel lorem", "07:26,04 3月"));
                    mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background4, null, "Vuctor Gregory Henderson", "Etiam pellentesque aliquet tellus", "20:34,04 3月"));
                }
                listContainer = (ListContainer) component.findComponentById(ResourceTable.Id_listdialogview);
                listContainer.setItemProvider(new DemosAdapter(mList, getContext()));
                listContainer.setComponentSize(900, 1800);
                commonDialog.show();
            }
        });

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("List dialog")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("List dialog")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("List dialog", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("List dialog", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("List dialog", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image1.setVisibility(Component.VISIBLE);
        image2.setVisibility(Component.VISIBLE);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("List dialog");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
